package com.client.data;


import com.client.service.data.CompagnieDataService;
import com.client.service.data.CompagnieDataServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.shared.model.Compagnie;
import com.shared.model.Produit;
import com.smartgwt.client.types.Autofit;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
/**
 * Extension d'une {@link ListGrid} aux {@link Produit}
 * @author queyroi
 *
 */
public class ProduitListGrid extends ListGrid{

	private  final CompagnieDataServiceAsync pDataService = GWT.create(CompagnieDataService.class);	
	
	public ProduitListGrid() {	
		
		
		ListGridField nomField = new ListGridField("nom", "Nom");
		nomField.setWidth(100);
		ListGridField typeField = new ListGridField("typeContrat", "Type Contrat");
		typeField.setWidth(100);
		ListGridField compagnieField = new ListGridField("compagnie", "Compagnie");
		compagnieField.setWidth(100);		
		ListGridField montantHonorairesField = new ListGridField("montantHonoraires", "Montant Honoraires");
		montantHonorairesField.setWidth(75);
		ListGridField fraisCourtageField = new ListGridField("fraisCourtage", "Frais Courtage");
		fraisCourtageField.setWidth(75);
		ListGridField montantCommissionsField = new ListGridField("montantCommissions", "Montant Commissions");
		montantCommissionsField.setWidth(75);
		ListGridField montantCourtageField = new ListGridField("montantCourtage", "Montant Courtage");
		montantCourtageField.setWidth(75);
		ListGridField montantCompagnieField = new ListGridField("montantCompagnie", "Montant Compagnie");
		montantCompagnieField.setWidth(75);
		ListGridField taxeAssuranceField = new ListGridField("taxeAssurance", "Taxe Assurance");
		taxeAssuranceField.setWidth(75);
		ListGridField montantCommissionCATNATField = new ListGridField("montantCommissionCATNAT", "Montant commission CATNAT");
		montantCommissionCATNATField.setWidth(75);			
		ListGridField montantHTField = new ListGridField("montantHT", "Montant Hors-Taxes");
		montantHTField.setWidth(75);
		ListGridField montantTTCField = new ListGridField("montantTTC", "Montant Toutes-Taxes");
		montantTTCField.setWidth(75);
		ListGridField montantTotalField = new ListGridField("montantTotal", "Montant Total");
		montantTotalField.setWidth(75);
		
		ListGridField[] fields = new ListGridField[]{nomField, typeField, compagnieField, montantHonorairesField,
				fraisCourtageField, montantCommissionsField, montantCourtageField, montantCompagnieField, taxeAssuranceField,
				montantCommissionCATNATField,montantHTField,montantTTCField,montantTotalField};
		setAlternateRecordStyles(true);
		setShowAllRecords(true);
		
		setFields(fields);	
		
		setHeight("300px");
		setAutoFitData(Autofit.HORIZONTAL);
		setAutoFitMaxWidth(670);		
		setCellHeight(40);
		setWrapCells(true);
		
	}
	
	
	public void addData(Produit data) {	
		final ListGridRecord record = new ListGridRecord();
		pDataService.getCompagnie(data.getCompagnie().getId(), new AsyncCallback<Compagnie>() {			
			public void onSuccess(Compagnie result) {
				record.setAttribute("compagnie", result.getNom());				
			}			
			public void onFailure(Throwable caught) {
				record.setAttribute("compagnie", "?");					
			}
		});		
		record.setAttribute("nom", data.getNom());
		record.setAttribute("typeContrat", data.getTypeContrat());		
		record.setAttribute("montantHonoraires", data.getTarification().getMontantHonoraires());
		record.setAttribute("montantCommissions", data.getTarification().getMontantCommissions());
		record.setAttribute("montantCourtage", data.getTarification().getMontantCourtage());
		record.setAttribute("montantCompagnie", data.getTarification().getMontantCompagnie());
		record.setAttribute("fraisCourtage", data.getTarification().getFraisCourtage());
		record.setAttribute("taxeAssurance", data.getTarification().getTaxeAssurance());
		record.setAttribute("montantCommissionCATNAT", data.getTarification().getMontantCommissionCATNAT());
		record.setAttribute("montantHT", data.getTarification().getMontantHT());
		record.setAttribute("montantTTC", data.getTarification().getMontantTTC());
		record.setAttribute("montantTotal", data.getTarification().getMontantTotal());
		addData(record);
	}
	
	public void removeAll(){
		selectAllRecords();
		removeSelectedData();
	}
	
	
}
